package com.softcloud.gamesbase;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class MainPage extends BaseActivity {

    private List<Game> games = new ArrayList<Game>();
    private ListView gameList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);
        gameInit();
        gameList = (ListView)findViewById(R.id.game_list);
        GameAdapter adapter = new GameAdapter(MainPage.this,R.layout.game_item,games);
        gameList.setAdapter(adapter);
        GameItemListener gameItemListener = new GameItemListener();
        gameList.setOnItemClickListener(gameItemListener);
    }

    class GameItemListener implements AdapterView.OnItemClickListener{
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Game game = games.get(position);
//            Toast.makeText(MainPage.this,game.getName(),Toast.LENGTH_SHORT).show();
            switch (game.getName()){
                case "Dota":
                    HeroPage.actionStart(MainPage.this,"Dota");
                    break;
                case "三国杀":
                    HeroPage.actionStart(MainPage.this,"三国杀");
                    break;
                case "炉石传说":
                    HeroPage.actionStart(MainPage.this,"炉石传说");
                    break;
            }
        }
    }

    private void gameInit(){
        Game dota = new Game("Dota",R.drawable.dota2);
        Game sanguosha = new Game("三国杀",R.drawable.sanguosha);
        Game healthstone = new Game("炉石传说",R.drawable.healthstone);
        games.add(dota);
        games.add(sanguosha);
        games.add(healthstone);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_page, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
